<?php
class Zend_Page_Paging
{
	public $total = 0;
	public $page = 1;
	public $limit = 20;
	public $num_links = 5;
	public $url = '';
	public $text = 'Có tất cả <strong>{start}</strong>/<strong>{pages}</strong> trang - <strong>{total}</strong> tin';
	public $text_first = 'Về đầu';
	public $text_last = 'Về cuối';
	public $text_next = 'Tiếp &gt;';
	public $text_prev = '&lt; Trước';
	public $style_links = '';
	public $style_results = 'results';
	public $none_page = '<span class="disabled">Về đầu</span> &nbsp;|&nbsp; <span class="disabled">&nbsp;&lt; Trước</span> &nbsp;|&nbsp;<strong>1</strong>&nbsp;| &nbsp;<a href="#">Tiếp &gt;</a> &nbsp;|&nbsp;<a href="#">Về cuối</a>';
	 
	public function render() {
		$total = $this->total;
		
		if ($this->page < 1) {
			$page = 1;
		} else {
			$page = $this->page;
		}
		
		if (!$this->limit) {
			$limit = 10;
		} else {
			$limit = $this->limit;
		}
		
		$num_links = $this->num_links;
		$num_pages = ceil($total / $limit);
		
		$output = '';
		
		if ($page > 1) {
			$output .= ' <a href="' . $this->url.'/page/1'. '">' . $this->text_first . '</a> <span>&nbsp;|&nbsp;</span> <a href="' .$this->url.'/page/'. ($page - 1) . '">' . $this->text_prev . '</a> <span>&nbsp;|&nbsp;</span>';
    	}

		if ($num_pages > 1) {
			if ($num_pages <= $num_links) {
				$start = 1;
				$end = $num_pages;
			} else {
				$start = $page - floor($num_links / 2);
				$end = $page + floor($num_links / 2);
			
				if ($start < 1) {
					$end += abs($start) + 1;
					$start = 1;
				}
						
				if ($end > $num_pages) {
					$start -= ($end - $num_pages);
					$end = $num_pages;
				}
			}

			if ($start > 1) {
				$output .= ' ';
			}

			for ($i = $start; $i <= $end; $i++) {
				if ($page == $i) {
					$output .= ' <span class="disabled">' . $i . '</span>';
				} else {
					$output .= ' <a href="' . $this->url.'/page/'.$i.'">' . $i . '</a> ';
				}	
			}
							
			if ($end < $num_pages) {
				$output .= ' ';
			}
		}
		
   		if ($page < $num_pages) {
			$output .= '<span>&nbsp;|&nbsp;</span> <a href="' .$this->url.'/page/'.( $page + 1) . '">' . $this->text_next . '</a> <span>&nbsp;|&nbsp;</span> <a href="' .$this->url.'/page/'.$num_pages . '">' . $this->text_last . '</a> ';
		}
		
		$find = array(
			'{start}',
			'{end}',
			'{total}',
			'{pages}'
		);
		
		$replace = array(
			$page,
			((($page - 1) * $limit) > ($total - $limit)) ? $total : ((($page - 1) * $limit) + $limit),
			$total, 
			$num_pages
		);
		
		return ($output ? '<div style="float:right;width:410px;text-align: right;padding-right: 9px;">' . ($output) . '</div>' : '<div style="float:right;width:410px;text-align: right;padding-right:9px">'.$this->none_page.'</div>') . '<div class="' . $this->style_results . '" style="float:left;width:250px;text-align: left;padding-left: 9px;">' . str_replace($find, $replace, $this->text) . '</div>';
	}
}
?>